//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by GivenBmli on 16/8/3.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kRedent @"redemt"
#define SjColor    [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
@interface ViewController ()<UICollectionViewDelegate , UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 
    UICollectionViewFlowLayout *flowLayount = [[UICollectionViewFlowLayout alloc]init];
    flowLayount.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds)- 5) / 2, 120);
    flowLayount.minimumLineSpacing = 5;
    flowLayount.minimumInteritemSpacing = 5;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayount];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kRedent];
//    关于CollectioinView LifeCycle
//    1.numberOfSectionsInCollectionView
//    2.collectionView:numberOfItemsInSection:
//    注意:3-5循环执行
//    3.prepareForReuse(从重用池取出时调用)
//    4.collectionView:cellForItemAtIndexPath:
//    5.collectionView: willDisplayCell:
//    当cell完全出屏幕时
//    6.collectionView: didEndDisplayingCell:
//    iOS 10以前
//    出屏幕立即返回,依旧进入重用池 3->4->5
//    iOS 10 出屏幕立即返回,会执行4->5,不会执行3,提高了效率

}
#pragma make - collectionView LifeCycle
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    区数
    NSLog(@"[%@:%s]",self,__FUNCTION__);
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"[%@:%s]",self,__FUNCTION__);
    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kRedent forIndexPath:indexPath];
        cell.contentView.backgroundColor = SjColor;
    return cell;
}

// ios 8.0后推出 cell将要显示在屏幕时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld@[%@:%s]",cell,indexPath.row,self,__FUNCTION__);
}

// ios 8.0后推出 cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld@[%@:%s]",cell,indexPath.row,self,__FUNCTION__);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
